/**
 * Moondance UML modeling tool
 * 
 * This program is free software; you can redistribute it and
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package ro.pub.cs.se.moondance.editpart.factory;

import org.eclipse.gef.palette.CombinedTemplateCreationEntry;
import org.eclipse.gef.palette.ConnectionCreationToolEntry;
import org.eclipse.gef.palette.MarqueeToolEntry;
import org.eclipse.gef.palette.PaletteContainer;
import org.eclipse.gef.palette.PaletteDrawer;
import org.eclipse.gef.palette.PaletteRoot;
import org.eclipse.gef.palette.PaletteToolbar;
import org.eclipse.gef.palette.PanningSelectionToolEntry;
import org.eclipse.gef.palette.ToolEntry;
import org.eclipse.gef.requests.CreationFactory;
import org.eclipse.gef.requests.SimpleFactory;
import org.eclipse.jface.resource.ImageDescriptor;

import ro.pub.cs.se.moondance.MoondancePlugin;
import ro.pub.cs.se.moondance.model.Connection;
import ro.pub.cs.se.moondance.model.UMLEndState;
import ro.pub.cs.se.moondance.model.UMLStartState;
import ro.pub.cs.se.moondance.model.UMLState;

/**
 * Utility class that can create a GEF Palette.
 * 
 * @see #createPalette()
 */
public final class StatePaletteFactory {

	/** Create the "UML" drawer. */
	public static PaletteContainer createUMLClassDrawer() {
		PaletteDrawer componentsDrawer = new PaletteDrawer(
				"State Diagram components");

		/**
		 * Insert into Pallete the components
		 */
		componentsDrawer.add(new CombinedTemplateCreationEntry("Start State",
				"Create new Satrt State", UMLStartState.class,
				new SimpleFactory(UMLStartState.class), ImageDescriptor
						.createFromFile(MoondancePlugin.class,
								"icons/state/start_state_small.gif"),
				ImageDescriptor.createFromFile(MoondancePlugin.class,
						"icons/state/start_state_small.gif")));

		componentsDrawer.add(new CombinedTemplateCreationEntry("End State",
				"Create new End State", UMLEndState.class, new SimpleFactory(
						UMLEndState.class), ImageDescriptor.createFromFile(
						MoondancePlugin.class,
						"icons/state/end_state_small.gif"), ImageDescriptor
						.createFromFile(MoondancePlugin.class,
								"icons/state/end_state_small.gif")));

		componentsDrawer.add(new CombinedTemplateCreationEntry("State",
				"Create new State", UMLState.class, new SimpleFactory(
						UMLState.class), ImageDescriptor.createFromFile(
						MoondancePlugin.class, "icons/state/state_small.gif"),
				ImageDescriptor.createFromFile(MoondancePlugin.class,
						"icons/state/state_small.gif")));

		return componentsDrawer;
	}

	/**
	 * Creates the PaletteRoot and adds all palette elements. Use this factory
	 * method to create a new palette for your graphical editor.
	 * 
	 * @return a new PaletteRoot
	 */
	public static PaletteRoot createPalette() {
		PaletteRoot palette = new PaletteRoot();
		palette.add(createToolsGroup(palette));
		palette.add(createUMLClassDrawer());
		return palette;
	}

	/** Create the "Tools" group. */
	public static PaletteContainer createToolsGroup(PaletteRoot palette) {
		PaletteToolbar toolbar = new PaletteToolbar("Tools");

		// Add a selection tool to the group
		ToolEntry tool = new PanningSelectionToolEntry();
		toolbar.add(tool);
		palette.setDefaultEntry(tool);

		// Add a marquee tool to the group
		toolbar.add(new MarqueeToolEntry());

		// Add (solid-line) connection tool
		tool = new ConnectionCreationToolEntry("Solid connection",
				"Create a solid-line connection", new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					// see ShapeEditPart#createEditPolicies()
					// this is abused to transmit the desired line style
					public Object getObjectType() {
						return Connection.SOLID_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(MoondancePlugin.class,
						"icons/connection_s16.gif"), ImageDescriptor
						.createFromFile(MoondancePlugin.class,
								"icons/connection_s24.gif"));
		toolbar.add(tool);

		// Add (dashed-line) connection tool
		tool = new ConnectionCreationToolEntry("Dashed connection",
				"Create a dashed-line connection", new CreationFactory() {
					public Object getNewObject() {
						return null;
					}

					// see ShapeEditPart#createEditPolicies()
					public Object getObjectType() {
						return Connection.DASHED_CONNECTION;
					}
				}, ImageDescriptor.createFromFile(MoondancePlugin.class,
						"icons/connection_d16.gif"), ImageDescriptor
						.createFromFile(MoondancePlugin.class,
								"icons/connection_d24.gif"));
		toolbar.add(tool);

		return toolbar;
	}

	/** Utility class. */
	public StatePaletteFactory() {
	}
}
